﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
using System.Diagnostics;

namespace Autofac
{
    public class StopWatchInterceptor : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            int i = 0;
            foreach (var item in invocation.Arguments)
            {
                i++;
                Console.WriteLine("参数{0} =>类型:{1}", i.ToString(), item.GetType().Name);
            }
            var watch = new Stopwatch();
            watch.Start();
            Console.WriteLine("-----------方法调用前------------");
            invocation.Proceed();
            Console.WriteLine("-----------方法调用后------------");
            watch.Stop();

            var str = string.Format("方法 => {0}, 耗时:{1}毫秒！", invocation.Method.Name, watch.ElapsedMilliseconds.ToString());
            Debug.WriteLine(str);
            Console.WriteLine(str);
        }
    }
}
